package domain

import (
	"AcsGo/internal/util"
	"fmt"
)

type BookInfoRequest struct {
	Code            util.Field
	TransactionDate util.Field
	InstitutionId   util.FieldPrefix
	ItemIdentifier  util.FieldPrefix
	SequenceNumber  util.FieldPrefix
}

func HandleBookInfoRequest(str string) (*BookInfoRequest, error) {
	if str == "" {
		return nil, fmt.Errorf("str is empty")
	}
	var query = BookInfoRequest{
		Code:            util.Field{Value: "17", Length: 2},
		TransactionDate: util.Field{Value: "", Length: 18},
		InstitutionId:   util.FieldPrefix{Value: "", Prefix: "AO", Suffix: util.PrefixLink},
		ItemIdentifier:  util.FieldPrefix{Value: "", Prefix: "AB", Suffix: util.PrefixLink},
		SequenceNumber:  util.FieldPrefix{Value: "", Prefix: "AY", Suffix: util.PrefixLink},
	}

	length := query.Code.Length + query.TransactionDate.Length
	if len(str) < length {
		return nil, fmt.Errorf("str is too short")
	}

	strTemp := query.Code.GetValue(str)
	strTemp = query.InstitutionId.GetValue(strTemp)
	strTemp = query.ItemIdentifier.GetValue(strTemp)
	_ = query.SequenceNumber.GetValue(strTemp)
	return &query, nil
}

type BookInfoResponse struct {
	Code              util.Field
	CirculationStatus util.Field
	Fill              util.Field
	TransactionDate   util.Field
	HoldQueueLength   util.FieldPrefix
	InstitutionId     util.FieldPrefix
	ItemHolder        util.FieldPrefix
	ItemIdentifier    util.FieldPrefix
	TitleIdentifier   util.FieldPrefix
	ItemAuthor        util.FieldPrefix
	ISBN              util.FieldPrefix
	Reservation       util.FieldPrefix
	MediaType         util.FieldPrefix
	ItemProperties    util.FieldPrefix
	CallNo            util.FieldPrefix
	CurrentType       util.FieldPrefix
	PermanentLocation util.FieldPrefix
	CurrentLocation   util.FieldPrefix
	ReservationRdid   util.FieldPrefix
	Publisher         util.FieldPrefix
	Subject           util.FieldPrefix
	PageNum           util.FieldPrefix
	ShelfNo           util.FieldPrefix
	Curlib            util.FieldPrefix
	BookRecno         util.FieldPrefix
	ScreenMessage     util.FieldPrefix
	PrintLine         util.FieldPrefix
	SequenceNumber    util.FieldPrefix
}

func (q BookInfoResponse) StringFormat() (string, error) {
	sb, err := util.ConcatenateFields(q)
	if err != nil {
		return "", err
	}
	return sb, nil
}

func InitBookInfoResponse(ci, bkbm, bkTitle, bkAuthor, isbn, price, kc, publish, subject, bn, pgNum string) *BookInfoResponse {

	var queryResponse = BookInfoResponse{
		Code:              util.Field{Value: "18", Length: 2},
		CirculationStatus: util.Field{Value: "", Length: 2},
		Fill:              util.Field{Value: "0001", Length: 4},
		TransactionDate:   util.Field{Value: util.GetCurrentTimeInFormat(), Length: 18},
		HoldQueueLength:   util.FieldPrefix{Value: "00000", Prefix: "CF", Suffix: util.PrefixLink},
		InstitutionId:     util.FieldPrefix{Value: "", Prefix: "AO", Suffix: util.PrefixLink},
		ItemHolder:        util.FieldPrefix{Value: "", Prefix: "AA", Suffix: util.PrefixLink},
		ItemIdentifier:    util.FieldPrefix{Value: "", Prefix: "AB", Suffix: util.PrefixLink},
		TitleIdentifier:   util.FieldPrefix{Value: "", Prefix: "AJ", Suffix: util.PrefixLink},
		ItemAuthor:        util.FieldPrefix{Value: "", Prefix: "AW", Suffix: util.PrefixLink},
		ISBN:              util.FieldPrefix{Value: "", Prefix: "AK", Suffix: util.PrefixLink},
		Reservation:       util.FieldPrefix{Value: "0", Prefix: "RE", Suffix: util.PrefixLink},
		MediaType:         util.FieldPrefix{Value: "001", Prefix: "CK", Suffix: util.PrefixLink},
		ItemProperties:    util.FieldPrefix{Value: "", Prefix: "CH", Suffix: util.PrefixLink},
		CallNo:            util.FieldPrefix{Value: "", Prefix: "KC", Suffix: util.PrefixLink},
		CurrentType:       util.FieldPrefix{Value: "", Prefix: "CT", Suffix: util.PrefixLink},
		PermanentLocation: util.FieldPrefix{Value: "", Prefix: "AQ", Suffix: util.PrefixLink},
		CurrentLocation:   util.FieldPrefix{Value: "", Prefix: "AP", Suffix: util.PrefixLink},
		ReservationRdid:   util.FieldPrefix{Value: "", Prefix: "BG", Suffix: util.PrefixLink},
		Publisher:         util.FieldPrefix{Value: "", Prefix: "PB", Suffix: util.PrefixLink},
		Subject:           util.FieldPrefix{Value: "", Prefix: "SJ", Suffix: util.PrefixLink},
		PageNum:           util.FieldPrefix{Value: "", Prefix: "PG", Suffix: util.PrefixLink},
		ShelfNo:           util.FieldPrefix{Value: "", Prefix: "KP", Suffix: util.PrefixLink},
		Curlib:            util.FieldPrefix{Value: "", Prefix: "CU", Suffix: util.PrefixLink},
		BookRecno:         util.FieldPrefix{Value: "", Prefix: "BN", Suffix: util.PrefixLink},
		ScreenMessage:     util.FieldPrefix{Value: "", Prefix: "AF", Suffix: util.PrefixLink},
		PrintLine:         util.FieldPrefix{Value: "", Prefix: "AG", Suffix: util.PrefixLink},
		SequenceNumber:    util.FieldPrefix{Value: "3AZD2A1", Prefix: "AY", Suffix: util.PrefixLink},
	}
	// 1802000120240623    180015CF00000|AA|AB179662|AJ人类探险|
	// AW方国荣编著|AK978-7-5168-0198-7|RE0|CK001|CH28.0/28.0|
	// KCN81-49/6|CT999_CN01]]中文图书|AQYGT003|APYGT003|BG|CS|AT0|
	// PB台海出版社|SJ探险|PG167页|BN109109|PN|AF|AG|AY0AZC05B
	queryResponse.CirculationStatus.Value = ci
	queryResponse.ItemIdentifier.Value = bkbm
	queryResponse.TitleIdentifier.Value = bkTitle
	queryResponse.ItemAuthor.Value = bkAuthor
	queryResponse.ISBN.Value = isbn
	queryResponse.ItemProperties.Value = price
	queryResponse.CallNo.Value = kc
	queryResponse.Publisher.Value = publish
	queryResponse.Subject.Value = subject
	queryResponse.BookRecno.Value = bn
	queryResponse.PageNum.Value = pgNum

	return &queryResponse
}
